
<?php
require(dirname(__FILE__) . '/includes/init1.php');
require(dirname(__FILE__) . '/includes/simple_html_dom.php');
set_time_limit(0);
$starturl = "http://www.yihaodian.com/ctg/searchPage/c25887-%E8%91%A1%E8%90%84%E9%85%92/b0/a-s1-v0-p{1,2}-price-d0-f04-m1-rt0-pid-k/";
$urllists = array();
if(preg_match("~\{(\d+),(\d+)\}~",$starturl,$pagenum)){ 
	$pagebegin = intval($pagenum[1]); 
	$pageend = intval($pagenum[2]);
	for(;$pagebegin<=$pageend;$pagebegin++){ 
		$url = str_replace($pagenum[0],$pagebegin,$starturl);
		array_push($urllists,$url); 
	} 
}
$ranking = 1;
$change = 0;
$time = time();
$sql = "select start from ".$ecs->table('yihaodian')." where id = (select max(id) from ".$ecs->table('yihaodian').")";
$start = $db->getOne($sql);
foreach($urllists as $index=>$url){
	$curl = curl_init(); 
	curl_setopt($curl, CURLOPT_URL,$url); 
	curl_setopt($curl, CURLOPT_REFERER, 'http://www.yihaodian.com'); 
	curl_setopt($curl,CURLOPT_COOKIE,'	unionType=1; uid=433588_test_; __utma=148959678.929533370.1345632607.1347851740.1347857982.21; __utmz=148959678.1346746997.15.7.utmcsr=localhost|utmccn=(referral)|utmcmd=referral|utmcct=/sdkok/statistics.php; provinceId=5; grouponAreaId=22; abtest=87; search_keyword_history=%E6%B3%95%E5%9B%BD%E7%BA%A2%E9%85%92%2C%E4%BD%B3%E6%A2%A6%2Casd%2C%E6%82%A6%E5%9B%BD%2C%E7%BA%A2%E9%85%92; search_browse_history=1200%2C2478460%2C4353367%2C2297209%2C1885104%2C1121538%2C2704539%2C4260600%2C2823693%2C965555; cart_cookie__f0f89e67-aca1-4d56-a3cf-31a5b79f7e41_getAllProductNum=1; cart_cookie__f0f89e67-aca1-4d56-a3cf-31a5b79f7e41="1,2349325x1x1"; msessionid=B6PMZR4Y23W5FDPGUG64AXHPN6CTA2BB; NSC_wjq-xfc-80=ffffffff090a1f2b45525d5f4f58455e445a4a4229a0; __utmc=148959678; JSESSIONID=7606441DA5BED10DB290B91D94B90A81; cart_cookie_uuid=f0f89e67-aca1-4d56-a3cf-31a5b79f7e41; __utmb=148959678.1.10.1347857982; tracker_msessionid=R9UQ5N871JG3HFXTEPHGDMZ3C46PDZFY');
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
	$result = curl_exec($curl); 
	curl_close($curl);
	$yhd_re = (array)json_decode($result);
	$str = $yhd_re['value'];
	$html = str_get_html($str);
	//print($html);
	foreach($html->find('.producteg') as $h){
		$goods_list = array();
		$html1 = str_get_html($h->innertext);
		foreach($html1->find('.product_pic') as $i=>$e){
			$goods_list[$i]['products_sn']= get_str($e->href,'http://www.yihaodian.com/product/','_');
			$goods_list[$i]['ranking']= $ranking;
			$ranking++;
		}

		foreach($html1->find('.product_pic img') as $o=>$t){
			$goods_list[$o]['products_name']= $t->title;
		}

		foreach($html1->find('.price a') as $j=>$c){
			$goods_list[$j]['commentcount'] = str_replace("条评价","",iconv('GB2312', 'UTF-8',$c->innertext)).'<br>';
		}
		foreach($html1->find('.price span strong') as $k=>$p){
			$goods_list[$k]['price'] =  str_replace('¥','',$p->plaintext);
		}
		foreach($html1->find('.storeName') as $m=>$n){
			if(empty($n->plaintext)){
				$provide = '一号店';
			}else{
				$provide = $n->plaintext;
			}
			$goods_list[$m]['provider'] = $provide;
			//echo $provide.'<br>';
		}
		foreach($goods_list as $goods){
			$db->query("insert into ".$ecs->table('yihaodian')." (products_name,products_sn,price,commentcount,provider,ranking,add_time,start) values('".$goods['products_name']."','".$goods['products_sn']."','".$goods['price']."','".$goods['commentcount']."','".$goods['provider']."','".($goods['ranking'])."','".$time."','".($start+1)."')");
			$pre_ranking = $db->getOne("select ranking from ".$ecs->table('yihaodian')." where products_sn='".$goods['products_sn']."' and start='".$start."'");
			if($goods['ranking']-$pre_ranking!=0){
				$change++;
			}
		}
		unset($goods_list);
	}
	$html->clear();
	$html1->clear();
	unset($html);
	unset($html1);
}
$db->query("insert into ".$ecs->table('yhd_change')." (add_time,change_num,products_num) values ('".$time."','".$change."','".($ranking-1)."')");

function get_str($str,$start_str,$end_str){
   $start_pos = strpos($str,$start_str)+strlen($start_str);  
   $end_pos = strpos($str,$end_str);  
   $c_str_l = $end_pos - $start_pos;  
   $contents = substr($str,$start_pos,$c_str_l);  
   return $contents;  
} 
?>